package com.caigou.component;

import com.caigou.bean.cms.entity.ArticleKeyword;
import com.caigou.bean.cms.entity.ArticleTitle;
import com.caigou.bean.cms.entity.Channel;
import com.caigou.bean.cms.template.ArticleKeywordTemplate;
import com.caigou.bean.cms.template.ChannelDataTemplate;
import com.caigou.bean.vo.ChannelTdkVO;
import com.caigou.mapper.cms.*;
import com.caigou.pojo.ArticleProductData;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Component
public class HtmlListPageBuilderComponent {
    @Value("${domain}")
    private String domain;
    @Value("${domainJunDui}")
    private String domainJunDui;
    @Value("${domainQiChe}")
    private String domainQiChe;
    @Value("${domainIT}")
    private String domainIT;
    @Value("${domainKongTiao}")
    private String domainKongTiao;
    @Value("${domainDianTi}")
    private String domainDianTi;
    @Value("${domainJiaJu}")
    private String domainJiaJu;
    @Value("${domainHuBei}")
    private String domainHuBei;
    @Value("${domainHeNan}")
    private String domainHeNan;
    @Value("${domainLiaoNing}")
    private String domainLiaoNing;
    @Value("${domainFuJian}")
    private String domainFuJian;
    @Value("${domainHaiNan}")
    private String domainHaiNan;
    @Value("${domainHeBei}")
    private String domainHeBei;// 河北
    @Value("${domainShanDong}")
    private String domainShanDong;// 山东
    @Value("${domainJiangSu}")
    private String domainJiangSu;// 江苏
    @Value("${domainShaanXi}")
    private String domainShaanXi;// 陕西
    @Value("${domainBeiJing}")
    private String domainBeiJing;// 北京
    @Value("${domainZheJiang}")
    private String domainZheJiang;// 浙江
    @Value("${domainYunNan}")
    private String domainYunNan;// 云南
    @Value("${domainGuiZhou}")
    private String domainGuiZhou;// 贵州
    @Value("${domainChongQing}")
    private String domainChongQing;// 重庆
    @Value("${domainTianJin}")
    private String domainTianJin;// 天津
    @Value("${domainShenZhen}")
    private String domainShenZhen;// 深圳
    @Value("${domainQingDao}")
    private String domainQingDao;// 青岛
    @Value("${domainDaLian}")
    private String domainDaLian;// 青岛
    @Value("${domainXiZang}")
    private String domainXiZang;// 西藏
    @Value("${domainXinJiang}")
    private String domainXinJiang;// 新疆
    @Value("${domainNingXia}")
    private String domainNingXia;// 宁夏
    @Value("${domainGuangXi}")
    private String domainGuangXi;// 广西
    @Value("${domainNeiMengGu}")
    private String domainNeiMengGu;// 内蒙古
    @Value("${domainGanSu}")
    private String domainGanSu;// 甘肃
    @Value("${domainBingTuan}")
    private String domainBingTuan;// 新疆生产建设兵团

    @Resource
    private TemplateComponent templateComponent;// 模板处理类
    @Resource
    private ChannelMapper channelMapper;// 频道mapper
    @Resource
    private ChannelDataMapper channelDataMapper;// 频道数据mapper
    @Resource
    private ArticleTitleMapper articleTitleMapper;// 文章标题列表mapper
    @Resource
    private ArticleKeywordMapper articleKeywordMapper;// 文章关键字列表mapper
    @Resource
    private ArticleProductMapper articleProductMapper;// 文章产品信息表mapper

    // 频道推送修改--更新静态页数据

    // 频道配置修改--更新静态页数据
    public String buildListPageConfigHtml(String plateId){
        Channel channel = new Channel();
        String templateCode = "";// 模板编号
        switch (plateId){
            case "100":// 全局设置
                templateCode = "TAGLIST";
                channel = channelMapper.loadChannelByPlate("101");
                break;
            case "101":// 网站首页
                templateCode = "HEADERLIST";
                channel = channelMapper.loadChannelByPlate(plateId);
                break;
            case "102":// 采购人
                templateCode = "PURCHASERLIST";
                channel = channelMapper.loadChannelByPlate(plateId);
                break;
            case "103":// 采购实务
                templateCode = "PRACTICELIST";
                channel = channelMapper.loadChannelByPlate(plateId);
                break;
            case "104":// 监管
                templateCode = "SUPERVISELIST";
                channel = channelMapper.loadChannelByPlate(plateId);
                break;
            case "105":// 研究院
                templateCode = "INSTITUTELIST";
                channel = channelMapper.loadChannelByPlate(plateId);
                break;
            case "106":// 军队
                templateCode = "ARMYLIST";
                channel = channelMapper.loadChannelByPlate(plateId);
                break;
            case "107":// 汽车
                templateCode = "AUTOMOBILELIST";
                channel = channelMapper.loadChannelByPlate(plateId);
                break;
            case "108":// it
                templateCode = "ITLIST";
                channel = channelMapper.loadChannelByPlate(plateId);
                break;
            case "109":// 电梯
                templateCode = "ELEVATORLIST";
                channel = channelMapper.loadChannelByPlate(plateId);
                break;
            case "110":// 空调
                templateCode = "CONDITIONERLIST";
                channel = channelMapper.loadChannelByPlate(plateId);
                break;
            case "111":// 家具
                templateCode = "FURNITURELIST";
                channel = channelMapper.loadChannelByPlate(plateId);
                break;
            case "201":
                templateCode = "HEBEILIST";
                channel = channelMapper.loadChannelByPlate(plateId);
                break;
            case "301":
                templateCode = "HENANLIST";
                channel = channelMapper.loadChannelByPlate(plateId);
                break;
            case "401":
                templateCode = "SHANDONGILIST";
                channel = channelMapper.loadChannelByPlate(plateId);
                break;
//            case 501:
            case "601":
                templateCode = "LIAONINGLIST";
                channel = channelMapper.loadChannelByPlate(plateId);
                break;
//            case 701:
//            case 801:
            case "901":
                templateCode = "JIANGSULIST";
                channel = channelMapper.loadChannelByPlate(plateId);
                break;
            case "1001":
                templateCode = "ZHEJIANGLIST";
                channel = channelMapper.loadChannelByPlate(plateId);
                break;
//            case 1101:
            case "1201":
                templateCode = "FUJIANLIST";
                channel = channelMapper.loadChannelByPlate(plateId);
                break;
//            case 1301:
            case "1401":// 湖北
                templateCode = "HUBEILIST";
                channel = channelMapper.loadChannelByPlate(plateId);
                break;
//            case 1501:
//            case 1601:
            case "1701":
                templateCode = "HAINANLIST";
                channel = channelMapper.loadChannelByPlate(plateId);
                break;
//            case 1801:
            case "1901":
                templateCode = "GUIZHOULIST";
                channel = channelMapper.loadChannelByPlate(plateId);
                break;
            case "2001":
                templateCode = "YUNNANLIST";
                channel = channelMapper.loadChannelByPlate(plateId);
                break;
            case "2101":
                templateCode = "SHAANXILIST";
                channel = channelMapper.loadChannelByPlate(plateId);
                break;
            case "2201":
                templateCode = "GANSULIST";
                channel = channelMapper.loadChannelByPlate(plateId);
                break;
//            case 2301:
            case "2401":
                templateCode = "BEIJINGLIST";
                channel = channelMapper.loadChannelByPlate(plateId);
                break;
            case "2501":
                templateCode = "TIANJINLIST";
                channel = channelMapper.loadChannelByPlate(plateId);
                break;
            case "2601":
                templateCode = "CHONGQINGLIST";
                channel = channelMapper.loadChannelByPlate(plateId);
                break;
//            case 2701:
            case "2801":
                templateCode = "NEIMENGGU";
                channel = channelMapper.loadChannelByPlate(plateId);
                break;
            case "2901":
                templateCode = "GUANGXILIST";
                channel = channelMapper.loadChannelByPlate(plateId);
                break;
            case "3001":
                templateCode = "NINGXIALIST";
                channel = channelMapper.loadChannelByPlate(plateId);
                break;
            case "3101":
                templateCode = "XINJIANGLIST";
                channel = channelMapper.loadChannelByPlate(plateId);
                break;
            case "3201":
                templateCode = "XIZANGLIST";
                channel = channelMapper.loadChannelByPlate(plateId);
                break;
            case "3301":
                templateCode = "BINGTUANLIST";
                channel = channelMapper.loadChannelByPlate(plateId);
                break;
            case "3401":
                templateCode = "DALIANLIST";
                channel = channelMapper.loadChannelByPlate(plateId);
                break;
            case "3501":
                templateCode = "QINGDAOLIST";
                channel = channelMapper.loadChannelByPlate(plateId);
                break;
//            case 3601:
//            case 3701:
            case "3801":
                templateCode = "SHENZHENLIST";
                channel = channelMapper.loadChannelByPlate(plateId);
                break;
        }
        // 处理不需要处理的频道ID
        if (templateCode.equals("")) return "";

        Map<String, Object> map = new HashMap<>();// 模板数据
        // 获取频道配置信息
        map.put("channel", channel);
        map.put("channelSite", channel.getChannelSite());
        map.put("channelSiteUrl", channel.getChannelSiteUrl());
        // map中设置domain
        map.put("domain", domain);
        map.put("domainJunDui", domainJunDui);
        map.put("domainQiChe", domainQiChe);
        map.put("domainIT", domainIT);
        map.put("domainDianTi", domainDianTi);
        map.put("domainKongTiao", domainKongTiao);
        map.put("domainJiaJu", domainJiaJu);
        map.put("domainHuBei", domainHuBei);
        map.put("domainHeNan", domainHeNan);
        map.put("domainLiaoNing", domainLiaoNing);
        map.put("domainFuJian", domainFuJian);
        map.put("domainHaiNan", domainHaiNan);
        map.put("domainHeBei", domainHeBei);
        map.put("domainShanDong", domainShanDong);
        map.put("domainJiangSu", domainJiangSu);
        map.put("domainShaanXi", domainShaanXi);
        map.put("domainBeiJing", domainBeiJing);
        map.put("domainZheJiang", domainZheJiang);
        map.put("domainYunNan", domainYunNan);
        map.put("domainGuiZhou", domainGuiZhou);
        map.put("domainChongQing", domainChongQing);
        map.put("domainTianJin", domainTianJin);
        map.put("domainShenZhen", domainShenZhen);
        map.put("domainQingDao", domainQingDao);
        map.put("domainDaLian", domainDaLian);
        map.put("domainXiZang", domainXiZang);
        map.put("domainXinJiang", domainXinJiang);
        map.put("domainNingXia", domainNingXia);
        map.put("domainGuangXi", domainGuangXi);
        map.put("domainNeiMengGu", domainNeiMengGu);
        map.put("domainGanSu", domainGanSu);
        map.put("domainBingTuan", domainBingTuan);
        String url = null;
        // 进行静态页生成方法调用
        try {
            url = templateComponent.createHtml3(templateCode, map);
            log.error("静态文件生成成功，生成地址是={}", url);
        } catch (IOException e){
            log.error("静态文件生成失败，失败原因是={}", e.getMessage());
        }
        return url;
    }


    /**
     * 统一处理列表数据(获取对应标题列表、关键词列表)
     * @param list  数据列表
     * @return 处理后的数据列表
     */
    private List<ChannelDataTemplate> disposeChannelDataList(List<ChannelDataTemplate> list, Integer titleType){
        for (ChannelDataTemplate info: list){
            // 遍历列表获取对应标题类型列表
            ArticleTitle articleTitle = articleTitleMapper.loadArticleTitle(info.getArticleId(),titleType);
            if (null != articleTitle){
                info.setArticleTitle(articleTitle.getArticleTitle());
                info.setArticleTitleImage(articleTitle.getArticleImage());
                info.setArticleTitleImageAlt(articleTitle.getArticleImageAlt());
            }
            // 遍历列表获取对应的关键词列表
            List<ArticleKeywordTemplate> articleKeyWordList = new ArrayList<>();
            if (!info.getArticleKeyWord().equals("")){
                String[] articleKeyword = info.getArticleKeyWord().split(",");
                for (String word: articleKeyword){
                    ArticleKeyword keyword = articleKeywordMapper.selectByPrimaryKey(Integer.valueOf(word));
                    if (null != keyword){
                        ArticleKeywordTemplate keyWord = new ArticleKeywordTemplate();
                        keyWord.setId(keyword.getId());
                        keyWord.setKeyword(keyword.getKeyword());
                        articleKeyWordList.add(keyWord);
                    }
                }
            }
            // 如果文章类型为产品类型获取产品信息数据
            if (info.getArticleType() == 7){
                ArticleProductData articleProduct = articleProductMapper.loadArticleProductData(info.getArticleId());
                if (null != articleProduct){
                    info.setProductName(articleProduct.getProductName());
                    info.setProductPrice(articleProduct.getProductPrice());
                    info.setProductModel(articleProduct.getProductModel());
                    info.setProductPurpose(articleProduct.getProductPurpose());
                    info.setProductImage(articleProduct.getProductImage());
                }
            }
            info.setArticleKeyWordList(articleKeyWordList);
        }
        return list;
    }

    /**
     * 统一处理模板存放数据
     * @param templateType  应用模板类型
     * @param plate         单频道类型频道编号
     * @param plateList     多频道类型频道编号集合体
     * @return  封装数据的MAP
     */
    private Map<String, Object> disposeTemplateMap(Integer templateType, String plate, List<String> plateList) {
        Map<String, Object> map = new HashMap<>();
        switch (templateType) {
            case 1:// 行业频道正常类型(单频道+单频道列表)
            case 2:// 行业频道(图表数据)类型（数量翻倍）
            case 3:// 行业频道(图表数据)类型（数量三倍）
                Channel channel = channelMapper.loadChannelByPlate(plate);
                List<ChannelDataTemplate> list = channelDataMapper.loadChannelDataByChannel(plate, channel.getChannelCount() * templateType);// 图表数据为配置数量的2被
                if (null != list) disposeChannelDataList(list, channel.getTitleType());
                map.put("list", list);
                map.put("channel", channel);
                break;
            case 4:// 行业频道特殊类型(要闻、专题、政策、解读、黑名单)多相同频道综合
                List<Channel> channelList = new ArrayList<>();
                // 页卡内容数据
                for (int i = 0; i < plateList.size(); i++) {
                    Channel plateChannel = channelMapper.loadChannelByPlate(plateList.get(i));
                    channelList.add(plateChannel);
                    List<ChannelDataTemplate> channelDataTemplateList = channelDataMapper.loadChannelDataByChannel(plateList.get(i), plateChannel.getChannelCount());
                    if (null != channelDataTemplateList) disposeChannelDataList(channelDataTemplateList, plateChannel.getTitleType());
                    map.put("list" + (i + 1), channelDataTemplateList);
                }
                map.put("channelList", channelList);
                break;
        }
        return map;
    }


    /**
     * 生成作者的文章列表静态页
     * @return
     */
    public String buildAuthorPostListPageHtml(){
        //获取频道首页的标题、关键词、描述等信息
        Channel channel = channelMapper.getChannelByPlateId("101");
        String templateCode = "AUTHORPOSTLIST";
        Map<String, Object> map = new HashMap<>();// 模板数据
        // 获取频道配置信息
        map.put("channel", channel);
        map.put("domain", domain);
        String url = null;
        // 进行静态页生成方法调用
        try {
            url = templateComponent.createHtml(templateCode, map);
            log.error("作者更多文章列表静态文件生成成功，生成地址是={}", url);
        } catch (IOException e){
            log.error(e.getMessage(), e);
        }
        return url;
    }
}
